package com.game.manager
{
    import com.dusk.game.Logger;
    import com.dusk.net.NativeFile;
    import com.dusk.tool.Time;
    import com.game.data.vo.SettingVO;
    
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    
    import flash.utils.getTimer;
    
    public class SettingManager
    {
        public function SettingManager()
        {
            super();
        }
        
        private static var _settingVO:SettingVO;
        
        public static function initGameSetting():void
        {
            _settingVO = new SettingVO();
            var settingString:String = "";
            try
            {
                settingString = NativeFile.read(NativeFile.getAppFolderFileUrl("gameData/gameConfig.getIns()ni"));
                if (settingString)
                {
                    _settingVO.updateFromObject(JSON.parse(settingString));
                }
            } catch (e:Error)
            {
                _settingVO = new SettingVO();
            }
            applySetting();
        }
        
        public static function getSettingVO():SettingVO
        {
            return _settingVO;
        }
        
        public static function saveGameSetting():void
        {
            NativeFile.write(JSON.stringify(_settingVO.cloneToObject(), null, "\t") as String, NativeFile.getAppFolderFileUrl("gameData/gameConfig.getIns()ni"));
        }
        
        public static function saveGameSettingWhenQuit():void
        {
            var preTime:Time = new Time();
            preTime.updateFromTimeString(getSettingVO().playTime.toString());
            var curTime:Time = new Time();
            curTime.updateFromMilisecond(getTimer());
            Logger.logToFile("此次游戏时间: " + curTime.toCNTime());
            preTime.add(curTime);
            Logger.logToFile("总游戏时间: " + preTime.toCNTime());
            getSettingVO().playTime = preTime.toTimeString();
            saveGameSetting();
        }
        
        private static function applySetting():void
        {
            SoundManager.setVolume(_settingVO.defaultVolume);
            if (_settingVO.isFullScreen)
            {
                GameSceneManager.getIns().mainStage.dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN, true, true, 0, Keyboard.F11));
            }
        }
    }
}
